import Mock from 'mockjs';
const Random = Mock.Random;
Random.increment();
Random.county(true);

/**
 * 模拟数据库表格设计
 * 供应商表：suppliers、客户表：customers、商品表：goods、
 * 入库记录表（入库对应供应商）：warehouse、
 * 订单记录表（出库对应客户）：orders
 * 物资（仓库货物和资金）：仓库物资表：material_warehouse
 * 资金物资表：material_funds
 * 负债（对应入库单，供应商对账）：未付清款项的入库单：liabilities_GRN、未付清款项的供应商：liabilities_suppliers（累计值）
 * 欠账（对应出库订单，客户对账）：欠账的订单：default_order、欠账的客户：default_customer(累计值)
 * 入货单列表：invoice_list
 * 出货单列表：delivery_list
 */
// 数据持久化
let db = {};
if (!global.db) {
  const data = Mock.mock({
    'suppliers|10': [{
      'name': () => { return '供应商名字' + Random.cname(); },
      'contact': () => { return Random.cname(); },
      'contact_way': /1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\d{8}/,
      'address': '@county(true)',
      'remark': () => { return Random.cword(10, 20); },
    }],
    'customers|10': [{
      'name': () => { return '客户名字' + Random.cname(); },
      'contact': () => { return Random.cname(); },
      'contact_way': /1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\d{8}/,
      'address': '@county(true)',
      'remark': () => { return Random.cword(10, 20); },
    }],
    'goods|10': [{
      'product_id': `PID${Random.datetime('yyyy')}@increment(0)`,
      'name': () => { return '商品名称' + Random.cname(); },
      'type': '商品类型',
      'unit': '个',
      'img': () => { return Random.image('30x30') },
    }],
    'warehouse|10': [{
      'commodity_code': `CC${Random.datetime('yyyyMMdd')}@increment(0)`,
      'supplier_name': () => { return '供应商名字' + Random.cname(); },
      'purchase_date': () => { return Random.datetime('yyyy-MM-dd HH:mm:ss') },
      'amount_payable|50-100': 1,
      'amount_paid|0-50': 1,
      'remark': () => { return '库存管理系统入货单备注' + Random.cword(5, 10) },
      'invoice_list|3': [{
        'product_name': () => { return '商品名称' + Random.cname(); },
        'number|10-100': 1,
        'unit': '箱',
        'unit_price|5-10': 1,
        'remark': () => { return '每项入库商品备注' + Random.cword(5, 10) },
      }],
    }],
    'orders|10': [{
      'order_number': `MDC${Random.now('yyyyMMdd')}@increment(0)`,
      'customer_name': () => { return '客户名称' + Random.cname(); },
      'amount_payable|50-100': 1,
      'amount_paid|0-50': 1,
      'remark': '出货订单备注',
      'order_time': () => { return Random.datetime('yyyy-MM-dd HH:mm:ss'); },
      'delivery_list|3': [{
        'product_name': () => { return '商品名称' + Random.cname(); },
        'number|10-100': 1,
        'unit': '盒',
        'unit_price|5-10': 1,
        'remark': () => { return '每项出货商品备注' + Random.cword(5, 10) },
      }],
    }],
    'material_warehouse|13': [{
      'product_id': `PID${Random.datetime('yyyy')}@increment(0)`,
      'product_name': () => { return '商品名称' + Random.cname(); },
      'type': '生活单品',
      'unit': '箱',
      'amount_storage|100-1000': 1,
      'amount_out|0-1000': 1,
      'amount_stock|0-1000': 1,
      'average_selling_price|10-20': 1,
      'inventory_funds|0-10000': 1,
    }],
    'material_funds|3': [{
      'product_id': `PID${Random.datetime('yyyy')}@increment(0)`,
      'product_name': () => { return '商品名称' + Random.cname(); },
      'type': '生活单品',
      'purchasing_price|1000-10000': 1,
      'sales_amount|0-10000': 1,
      'profit_amount|0-10000': 1,
    }],
    'liabilities_GRN|10': [{
      'commodity_code': `CC${Random.datetime('yyyyMMdd')}@increment(0)`,
      'supplier_name': () => { return '供应商名字' + Random.cname(); },
      'purchase_date': () => { return Random.datetime('yyyy-MM-dd HH:mm:ss') },
      'amount_payable|50-100': 1,
      'amount_paid|0-50': 1,
      'amount_debt|0-50': 1,
      'product_list|3': [{
        'product_name': () => { return '商品名称' + Random.cname(); },
        'number|10-100': 1,
        'unit': '箱',
        'unit_price|5-10': 1,
        'amount_payable|50-100': 1,
        'amount_paid|0-50': 1,
        'amount_debt|0-50': 1,
        'remark': '每项入库商品资金备注',
      }],
    }],
    'liabilities_suppliers|10': [{
      'supplier_name': () => { return '供应商名字' + Random.cname(); },
      'amount_payable|50-100': 1,
      'amount_paid|0-50': 1,
      'amount_debt|0-50': 1,
    }],
    'default_order|10': [{
      'order_number': `MDC${Random.now('yyyyMMdd')}@increment(0)`,
      'order_time': () => { return Random.datetime('yyyy-MM-dd HH:mm:ss'); },
      'customer_name': () => { return '客户名字' + Random.cname(); },
      'amount_payable|50-100': 1,
      'amount_paid|0-50': 1,
      'amount_debt|0-50': 1,
      'product_list|3': [{
        'product_name': () => { return '商品名称' + Random.cname(); },
        'number|10-100': 1,
        'unit': '箱',
        'unit_price|5-10': 1,
        'amount_payable|50-100': 1,
        'amount_paid|0-50': 1,
        'amount_debt|0-50': 1,
        'remark': () => { return '每项欠款商品备注' + Random.cword(5, 10) },
      }],
    }],
    'default_customer|10': [{
      'customer_name': () => { return '客户名字' + Random.cname(); },
      'amount_payable|50-100': 1,
      'amount_paid|0-50': 1,
      'amount_debt|0-50': 1,
    }],
  });
  db = data;
  global.db = db;
} else {
  db = global.db;
}

module.exports = {
  [`GET /api/orders`](req, res) {
    res.status(200).json({
      message: 'success',
      orders: db.orders
    });
  },
  [`GET /api/warehouse`](req, res) {
    res.status(200).json({
      message: 'success',
      warehouse: db.warehouse
    });
  },
  [`GET /api/material`](req, res) {
    res.status(200).json({
      message: 'success',
      material_warehouse: db.material_warehouse,
      material_funds: db.material_funds
    });
  },
  [`GET /api/check/customers`](req, res) {
    res.status(200).json({
      message: 'success',
      default_order: db.default_order,
      default_customer: db.default_customer
    });
  },
  [`GET /api/check/suppliers`](req, res) {
    res.status(200).json({
      message: 'success',
      liabilities_GRN: db.liabilities_GRN,
      liabilities_suppliers: db.liabilities_suppliers
    });
  },
  [`GET /api/manage/customers`](req, res) {
    res.status(200).json({
      message: 'success',
      customers: db.customers,
    });
  },
  [`GET /api/manage/suppliers`](req, res) {
    res.status(200).json({
      message: 'success',
      suppliers: db.suppliers,
    });
  },
  [`GET /api/manage/goods`](req, res) {
    res.status(200).json({
      message: 'success',
      goods: db.goods,
    });
  },
}